package com.googlecode.jmicroerp.wicket.zzz;

import com.googlecode.jmicroerp.wicket.DefaultViewPage;
import com.googlecode.jmicroerp.model.ZZZ;
import com.googlecode.jmicroerp.model.Magazin;

import com.googlecode.jmicroerp.service.GenericService;
import com.googlecode.jmicroerp.repository.GenericProperty;
import com.googlecode.jmicroerp.repository.GenericRestriction;
import com.googlecode.jmicroerp.service.GenericService;

import com.googlecode.jmicroerp.wicket.DefaultEditPage;

import org.apache.wicket.request.mapper.parameter.PageParameters;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;

import java.util.Date;
import java.util.ArrayList;
import java.util.List;
import java.math.BigDecimal;

import org.apache.wicket.extensions.markup.html.form.DateTextField;
import org.apache.wicket.extensions.yui.calendar.DatePicker;

import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;


import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.Model;

import org.apache.wicket.spring.injection.annot.SpringBean;
import org.hibernate.SessionFactory;

/**
 *
 * @author Ionel.David
 */
public class ZZZAddPage extends DefaultEditPage {
    @SpringBean(name = "genericService")
    private GenericService<ZZZ> zzzService;
    @SpringBean(name = "genericService")
    private GenericService<Magazin> magazinService;
    
    private ZZZ zzz;
        
    public ZZZAddPage(Long magazinId) {
        Magazin magazin = magazinService.getById(Magazin.class, magazinId);
        // Recipient recipient = recipientService.getById(Recipient.class, recipientId);
        zzz = new ZZZ();
        zzz.setDataAdaugarii(new Date());
        zzz.setMagazin(magazin);
        
        Form<ZZZ> form = new Form<ZZZ>("form");
        form.setModel(new CompoundPropertyModel<ZZZ>(zzz));
        
        // form.add(new Label("magazin", recipient.getMagazin().getDenumire()));
        form.add(new Label("magazin", magazin.getDenumire()));
        // form.add(new Label("recip", recipient.getDenumire()));
        
        DateTextField dtfDataAdaugarii = new DateTextField("dataAdaugarii", "dd.MM.yyyy");
        dtfDataAdaugarii.add(new DatePicker());
        dtfDataAdaugarii.setRequired(true);
        form.add(dtfDataAdaugarii);
        
        TextField<BigDecimal> tfValoare = new TextField<BigDecimal>("valoareTotala");
        tfValoare.setRequired(true);
        form.add(tfValoare);
        
        TextField<String> tfDenumire = new TextField<String>("denumire");
        tfDenumire.setRequired(true);
        form.add(tfDenumire);
        
        SubmitLink salvare = new SubmitLink("submit") {

            @Override
            public void onSubmit() {
                if (zzz.getId() == null) {
                    zzzService.insert(zzz);
                } else {
                    zzzService.update(zzz);
                }
                setResponsePage(ZZZListPage.class);
            }
            
        };
        form.add(salvare);
        
        form.add(new Link("renuntare") {

            @Override
            public void onClick() {
                setResponsePage(ZZZListPage.class);
            }
            
        });

        add(form);
    }
}
